package com.v2future.sjt.collegecheckingattendance.common.activity;

import android.os.Build;
import android.text.TextUtils;
import android.webkit.WebSettings;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.widget.ProgressWebView;

/**
 * web页面
 * url - 需要加载的地址
 * @author shijianting
 * @time 2016/3/28 19:29
 */
public class WebViewActivity extends MainActivity{

    private ProgressWebView web;

    /**
     * 需要加载的地址
     */
    private String url = "";

    /**
     * 加载的web数据
     */
    private String urlData = "";

    /**
     * 传递的网页标题
     */
    private String title = "";

    /**
     * init first
     */
    @Override
    public void init() {
        url = getIntent().getStringExtra(IntentKey.WEB_URL);
        urlData = getIntent().getStringExtra(IntentKey.WEB_URL_DATA);
        title = getIntent().getStringExtra(IntentKey.WEB_TITLE);
    }

    /**
     * init view
     */
    @Override
    public void initView() {
        setContentView(R.layout.activity_webview);
        if (!TextUtils.isEmpty(title)) {
            setTitleText(title);
        }
        initTitleBack(null);
        web = (ProgressWebView) findViewById(R.id.webview1);
        web.addJavascriptInterface(new JavaScriptToMobileInterface(),
                "JavaScriptToMobileInterface");
        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setDomStorageEnabled(true);
        web.setHorizontalScrollBarEnabled(false);// 水平不显示
        web.setVerticalScrollBarEnabled(false); // 垂直不显示
        web.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        web.getSettings().setLoadWithOverviewMode(true);
        // webview漏洞,android3.0~android4.2移除searchBoxJavaBridge
        if (Build.VERSION.SDK_INT > 10 && Build.VERSION.SDK_INT < 17) {
            web.removeJavascriptInterface("searchBoxJavaBridge_");
        }
    }

    /**
     * init after
     */
    @Override
    public void initAfter() {
        if (!TextUtils.isEmpty(url)) {
            web.loadUrl(url);
        }
        else if (!TextUtils.isEmpty(urlData)) {
            web.loadDataWithBaseURL(null, urlData, "text/html", "utf-8",
                    null);
        }
    }

    /**
     * js方法回调使用
     */
    public class JavaScriptToMobileInterface {

    }
}
